﻿//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2015 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;
using System.Collections;
using Engine.Entities.Character;

/// <summary>
/// UIDragDropItem is a base script for your own Drag & Drop operations.
/// </summary>

[AddComponentMenu("NGUI/Interaction/Couple Drag and Drop Item")]
public class CoupleDragDropItem : MonoBehaviour
{
    public FrenzyPeopleController peopleCtrl;

    //EDIT
    public UISlider sliderAVG;
    public UILabel labelAVG;

    public UISlider sliderINT;
    public UILabel  labelINT;

    public UISlider sliderRES;
    public UILabel labelRES;

    public UISlider sliderSTR;
    public UILabel labelSTR;

    public UISlider sliderAGL;
    public UILabel labelAGL;

    public UISlider sliderCHA;
    public UILabel labelCHA;

    public UISlider sliderAPL;
    public UILabel labelAPL;

    public UISprite LogoSprite;
    public UIAtlas FrenzyAtlas;

    public const string atlasMale = "male";
    public const string atlasFemale = "girl";

    internal UICoupleManager uicouplemanager;
    internal SteveModel Model;
    //END EDIT

    protected virtual void Update()
    {
        if (restriction == Restriction.PressAndHold)
        {
            if (mPressed && !mDragging && mDragStartTime < RealTime.time)
                StartDragging();
        }
    }

    private float doubleClickStart = 0;

    void OnMouseUp()
    {
        if ((Time.time - doubleClickStart) < 0.3f)
        {
            this.OnDoubleClick();
            doubleClickStart = -1;
        }
        else
        {
            doubleClickStart = Time.time;
        }
    }

    void OnDoubleClick()
    {
        uicouplemanager.AddSelectedItem(Model, gameObject);
    }


    public enum Restriction
    {
        None,
        Horizontal,
        Vertical,
        PressAndHold,
    }

    /// <summary>
    /// What kind of restriction is applied to the drag & drop logic before dragging is made possible.
    /// </summary>

    public Restriction restriction = Restriction.None;

    /// <summary>
    /// Whether a copy of the item will be dragged instead of the item itself.
    /// </summary>

    public bool cloneOnDrag = false;

    /// <summary>
    /// How long the user has to press on an item before the drag action activates.
    /// </summary>

    [HideInInspector]
    public float pressAndHoldDelay = 1f;

    /// <summary>
    /// Whether this drag & drop item can be interacted with. If not, only tooltips will work.
    /// </summary>

    public bool interactable = true;

    #region Common functionality

    [System.NonSerialized]
    protected Transform mTrans;
    [System.NonSerialized]
    protected Transform mParent;
    [System.NonSerialized]
    protected Collider mCollider;
    [System.NonSerialized]
    protected Collider2D mCollider2D;
    [System.NonSerialized]
    protected UIButton mButton;
    [System.NonSerialized]
    protected UIRoot mRoot;
    [System.NonSerialized]
    protected UIGrid mGrid;
    [System.NonSerialized]
    protected UITable mTable;
    [System.NonSerialized]
    protected float mDragStartTime = 0f;
    [System.NonSerialized]
    protected UIDragScrollView mDragScrollView = null;
    [System.NonSerialized]
    protected bool mPressed = false;
    [System.NonSerialized]
    protected bool mDragging = false;
    [System.NonSerialized]
    protected UICamera.MouseOrTouch mTouch;

    /// <summary>
    /// Cache the transform.
    /// </summary>

    protected virtual void Start()
    {
        mTrans = transform;
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
		mCollider = collider;
		mCollider2D = collider2D;
#else
        mCollider = gameObject.GetComponent<Collider>();
        mCollider2D = gameObject.GetComponent<Collider2D>();
#endif
        mButton = GetComponent<UIButton>();
        mDragScrollView = GetComponent<UIDragScrollView>();

        GameObject tmpuicouplemanager = GameObject.FindGameObjectWithTag("Coupling");
        if ( tmpuicouplemanager != null )
        {
            uicouplemanager = GameObject.FindGameObjectWithTag("Coupling").GetComponent<UICoupleManager>();
        }

    }

    /// <summary>
    /// Record the time the item was pressed on.
    /// </summary>

    protected virtual void OnPress(bool isPressed)
    {
        if (!interactable) return;

        if (isPressed)
        {
            mTouch = UICamera.currentTouch;
            mDragStartTime = RealTime.time + pressAndHoldDelay;
            mPressed = true;
        }
        else
        {
            mPressed = false;
            mTouch = null;
        }
    }

    /// <summary>
    /// Start the dragging operation after the item was held for a while.
    /// </summary>

    

    /// <summary>
    /// Start the dragging operation.
    /// </summary>

    protected virtual void OnDragStart()
    {
        if (!interactable) return;
        if (!enabled || mTouch != UICamera.currentTouch) return;

        // If we have a restriction, check to see if its condition has been met first
        if (restriction != Restriction.None)
        {
            if (restriction == Restriction.Horizontal)
            {
                Vector2 delta = mTouch.totalDelta;
                if (Mathf.Abs(delta.x) < Mathf.Abs(delta.y)) return;
            }
            else if (restriction == Restriction.Vertical)
            {
                Vector2 delta = mTouch.totalDelta;
                if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y)) return;
            }
            else if (restriction == Restriction.PressAndHold)
            {
                // Checked in Update instead
                return;
            }
        }
        StartDragging();
    }

    /// <summary>
    /// Start the dragging operation.
    /// </summary>

    protected virtual void StartDragging()
    {
        if (!interactable) return;

        if (!mDragging)
        {
            if (cloneOnDrag)
            {
                mPressed = false;
                GameObject clone = NGUITools.AddChild(transform.parent.gameObject, gameObject);
                clone.transform.localPosition = transform.localPosition;
                clone.transform.localRotation = transform.localRotation;
                clone.transform.localScale = transform.localScale;

                UIButtonColor bc = clone.GetComponent<UIButtonColor>();
                if (bc != null) bc.defaultColor = GetComponent<UIButtonColor>().defaultColor;

                if (mTouch != null && mTouch.pressed == gameObject)
                {
                    mTouch.current = clone;
                    mTouch.pressed = clone;
                    mTouch.dragged = clone;
                    mTouch.last = clone;
                }

                CoupleDragDropItem item = clone.GetComponent<CoupleDragDropItem>();
                item.mTouch = mTouch;
                item.mPressed = true;
                item.mDragging = true;
                item.Start();
                item.OnDragDropStart();
                item.Model = Model; //copio anche il contenuto

                if (UICamera.currentTouch == null)
                    UICamera.currentTouch = mTouch;

                mTouch = null;

                UICamera.Notify(gameObject, "OnPress", false);
                UICamera.Notify(gameObject, "OnHover", false);

                
            }
            else
            {
                mDragging = true;
                OnDragDropStart();
            }
        }
    }

    /// <summary>
    /// Perform the dragging.
    /// </summary>

    protected virtual void OnDrag(Vector2 delta)
    {
        if (!interactable) return;
        if (!mDragging || !enabled || mTouch != UICamera.currentTouch) return;
        OnDragDropMove(delta * mRoot.pixelSizeAdjustment);
    }

    /// <summary>
    /// Notification sent when the drag event has ended.
    /// </summary>

    protected virtual void OnDragEnd()
    {
        if (!interactable) return;
        if (!enabled || mTouch != UICamera.currentTouch) return;
        StopDragging(UICamera.hoveredObject);
    }

    /// <summary>
    /// Drop the dragged item.
    /// </summary>

    public void StopDragging(GameObject go)
    {
        if (mDragging)
        {
            mDragging = false;
            OnDragDropRelease(go);
        }
    }

    #endregion

    /// <summary>
    /// Perform any logic related to starting the drag & drop operation.
    /// </summary>

    protected virtual void OnDragDropStart()
    {
        // Automatically disable the scroll view
        if (mDragScrollView != null) mDragScrollView.enabled = false;

        // Disable the collider so that it doesn't intercept events
        if (mButton != null) mButton.isEnabled = false;
        else if (mCollider != null) mCollider.enabled = false;
        else if (mCollider2D != null) mCollider2D.enabled = false;

        mParent = mTrans.parent;
        mRoot = NGUITools.FindInParents<UIRoot>(mParent);
        mGrid = NGUITools.FindInParents<UIGrid>(mParent);
        mTable = NGUITools.FindInParents<UITable>(mParent);

        // Re-parent the item
        if (UIDragDropRoot.root != null)
            mTrans.parent = UIDragDropRoot.root;

        Vector3 pos = mTrans.localPosition;
        pos.z = 0f;
        mTrans.localPosition = pos;

        TweenPosition tp = GetComponent<TweenPosition>();
        if (tp != null) tp.enabled = false;

        SpringPosition sp = GetComponent<SpringPosition>();
        if (sp != null) sp.enabled = false;

        // Notify the widgets that the parent has changed
        NGUITools.MarkParentAsChanged(gameObject);

        if (mTable != null) mTable.repositionNow = true;
        if (mGrid != null) mGrid.repositionNow = true;
    }

    /// <summary>
    /// Adjust the dragged object's position.
    /// </summary>

    protected virtual void OnDragDropMove(Vector2 delta)
    {
        mTrans.localPosition += (Vector3)delta;
    }

    /// <summary>
    /// Drop the item onto the specified object.
    /// </summary>

    protected virtual void OnDragDropRelease(GameObject surface)
    {
        if (!cloneOnDrag)
        {
            // Re-enable the collider
            if (mButton != null) mButton.isEnabled = true;
            else if (mCollider != null) mCollider.enabled = true;
            else if (mCollider2D != null) mCollider2D.enabled = true;

            // Is there a droppable container?
            UIDragDropContainer container = surface ? NGUITools.FindInParents<UIDragDropContainer>(surface) : null;

            if (container != null)
            {
                // Container found -- parent this object to the container
                mTrans.parent = (container.reparentTarget != null) ? container.reparentTarget : container.transform;

                Vector3 pos = mTrans.localPosition;
                pos.z = 0f;
                mTrans.localPosition = pos;
            }
            else
            {
                // No valid container under the mouse -- revert the item's parent
                mTrans.parent = mParent;
            }

            // Update the grid and table references
            mParent = mTrans.parent;
            mGrid = NGUITools.FindInParents<UIGrid>(mParent);
            mTable = NGUITools.FindInParents<UITable>(mParent);

            // Re-enable the drag scroll view script
            if (mDragScrollView != null)
                StartCoroutine(EnableDragScrollView());

            // Notify the widgets that the parent has changed
            NGUITools.MarkParentAsChanged(gameObject);

            if (mTable != null) mTable.repositionNow = true;
            if (mGrid != null) mGrid.repositionNow = true;

            // We're now done
            OnDragDropEnd();
        }
        else
        {
            //EDIT
            if ( uicouplemanager.IsValidOperation( gameObject , surface, Model ) )
            {
                Debug.Log("copio il componente");
                uicouplemanager.AddSelectedItem(Model, gameObject);
            }
            
            NGUITools.Destroy(gameObject);

        }
    }

    /// <summary>
    /// Function called when the object gets reparented after the drop operation finishes.
    /// </summary>

    protected virtual void OnDragDropEnd() { }

    /// <summary>
    /// Re-enable the drag scroll view script at the end of the frame.
    /// Reason: http://www.tasharen.com/forum/index.php?topic=10203.0
    /// </summary>

    protected IEnumerator EnableDragScrollView()
    {
        yield return new WaitForEndOfFrame();
        if (mDragScrollView != null) mDragScrollView.enabled = true;
    }

    internal void RefreshUI()
    {
        labelINT.text = Model.Skills.GetValue(SkillsEnum.Intelligence).ToString();
        labelRES.text = Model.Skills.GetValue(SkillsEnum.Resistance).ToString();
        labelSTR.text = Model.Skills.GetValue(SkillsEnum.Strenght).ToString();
        labelAGL.text = Model.Skills.GetValue(SkillsEnum.Agility).ToString();
        labelCHA.text = Model.Skills.GetValue(SkillsEnum.Charm).ToString();
        labelAPL.text = Model.Skills.GetValue(SkillsEnum.Appeal).ToString();

        if (Model.Gender == GenderEnum.Male)
            LogoSprite.spriteName = "male";
        else
            LogoSprite.spriteName = "girl";

        sliderINT.value = peopleCtrl.ToPercentage(SkillsEnum.Intelligence, Model.Skills);
        sliderRES.value = peopleCtrl.ToPercentage(SkillsEnum.Resistance, Model.Skills);
        sliderSTR.value = peopleCtrl.ToPercentage(SkillsEnum.Strenght, Model.Skills);
        sliderAGL.value = peopleCtrl.ToPercentage(SkillsEnum.Agility, Model.Skills);
        sliderCHA.value = peopleCtrl.ToPercentage(SkillsEnum.Charm, Model.Skills);
        sliderAPL.value = peopleCtrl.ToPercentage(SkillsEnum.Appeal, Model.Skills);

        float avg = peopleCtrl.GetAVGSkills(Model.Skills);
        sliderAVG.value = avg;
        labelAVG.text = avg.ToString("0.0");

    }
}
